package mest.service.cxj.impl;

import mest.VO.cxj.FinancialCostOrderRecordVO;
import mest.dao.cxj.FinancialCostOrderDao;
import mest.dao.cxj.PlanCostOrderDao;
import mest.entity.FinancialCostOrder;
import mest.entity.FinancialCostOrderRecord;
import mest.entity.PlanCostOrder;
import mest.util.MoneyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import mest.dao.cxj.FinancialCostOrderRecordDao;
import mest.service.cxj.FinancialCostOrderRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FinancialCostOrderRecordServiceImpl implements FinancialCostOrderRecordService {

    @Resource
    private FinancialCostOrderRecordDao financialCostorderRecordDao;

    @Resource
    private PlanCostOrderDao planCostOrderDao;

    @Resource
    private FinancialCostOrderDao financialCostOrderDao;


    @Override
    public Integer insertFinancialCostOrderRecord(FinancialCostOrderRecord fcor, String theirDb) throws Exception {
        FinancialCostOrder financialCostOrder = financialCostOrderDao.selectFinancialCostOrderById(fcor.getFcId(), theirDb);
        PlanCostOrder planCostOrder = planCostOrderDao.selectPlanCostOrderById(financialCostOrder.getPcId(), theirDb);
        BigDecimal financialMoney = MoneyUtil.add(financialCostOrder.getHasBennMoney(), fcor.getThisMoney()); //实际付款
        BigDecimal planMoney = MoneyUtil.add(planCostOrder.getHasBennMoney(), fcor.getThisMoney());  //计划付款
        Integer i1 = financialCostOrderDao.updateFinancialCostOrderByMoney(fcor.getFcId(), financialMoney, theirDb);
        Integer i2 = planCostOrderDao.updatePlanCostOrderByMoney(financialCostOrder.getPcId(), planMoney, theirDb);
        if (i1 > 0 && i2 > 0) {
            return financialCostorderRecordDao.insertFinancialCostOrderRecord(fcor, theirDb);
        } else {
            throw new Exception("系统异常,请联系管理员");
        }
    }

    @Override
    public List<FinancialCostOrderRecordVO> selectAllFinancialCostOrderRecordById(Integer page, Integer limit, Integer fcId, String theirDb) {
        return financialCostorderRecordDao.selectAllFinancialCostOrderRecordById((page - 1) * limit, limit, fcId, theirDb);
    }

    @Override
    public Integer selectCountFinancialCostOrderRecordById(Integer fcId, String theirDb) {
        return financialCostorderRecordDao.selectCountFinancialCostOrderRecordById(fcId, theirDb);
    }
}
